package com.students.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.students.pojo.Localstorage;
import com.students.pojo.RespBean;
import com.students.pojo.RespPageBean;
import com.students.service.ILocalstorageService;
import com.students.utils.AdminUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.FilenameUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.management.relation.Role;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author caohanqing
 * @since 2022-07-10
 */
@Api(tags = "本地储存管理-Localstorage")
@RestController
@RequiredArgsConstructor
@RequestMapping("/localstorage")
public class LocalstorageController {
    private final ILocalstorageService localstorageService;

    @ApiOperation(value = "上传文件")
    @PostMapping("/Bupload")
    @ApiImplicitParams({@ApiImplicitParam(name="file",value = "上传文件",dataType = "MultipartFile")})
    public RespBean upload(@RequestParam(value="fileid") String fileid,@RequestParam(value="newname")  String newname, MultipartFile file) throws IOException {
        if (file==null){
            return RespBean.error("无法上传空文件");
        }

        //获取文件类型
        String contentType = file.getContentType();
        int prefix = FilenameUtils.indexOfLastSeparator(contentType);
        String substring = contentType.substring(0, prefix);

        String imgFound = null;
        if (substring.equals("image")){
            imgFound = localstorageService.ImgFound(file);
        }else
        if (substring.equals("application")&&substring.equals("text")){
            imgFound = localstorageService.TxtFound(file);
        }else if (StringUtils.isEmpty(substring)){
            return RespBean.error("文件错误！无法上传");
        }else if (!StringUtils.isEmpty(substring)){
            imgFound = localstorageService.DateFound(file);
        }

        double l = (double) file.getSize() / 1024 / 1024 ;
        String format = String.format("%.2f", l);
        if (l>100){
            return RespBean.error("上传文件须小于100MB！");
        }

        if (newname==null){
            newname= file.getOriginalFilename();
        }

        //上传文件至数据库
        /**
         * 注意：修改者属性是以后端内部的操作者工具传递id，创建者属性是以前端传来的id参数
         */
        Localstorage localstorage=new Localstorage();
        localstorage.setRealName(file.getOriginalFilename())
                .setName(newname)
                .setSuffix("."+FilenameUtils.getExtension(file.getOriginalFilename()))
                .setPath(":/"+imgFound)
                .setType(contentType)
                .setDownload(0)
                .setSize(format+"MB")
                .setCreateBy(Integer.parseInt(fileid))
                .setUpdateTime(LocalDateTime.now())
                .setCreateTime(LocalDateTime.now())
                .setUpdateBy(AdminUtil.getCurrentAdmin().getId())
                .setStatus(0)
                .setCola(substring.equals("image")?0:1);
        localstorageService.save(localstorage);//文件最终插入数据库
        return RespBean.success("上传成功");
    }

    @ApiOperation(value = "查询本地所有文件列表")
    @GetMapping("/findList")
    public List<Localstorage> getFindAlls(){
        return localstorageService.list();
    }

    @ApiOperation(value = "根据用户id查询本地文件列表(JSON格式)")
    @GetMapping("/findListJSON")
    public List<Localstorage> getFindAllsJson(){
        return localstorageService.list();
    }

    @ApiOperation(value = "根据用户id和上级目录分段查询文件列表")
    @GetMapping("/findListID")
    public List<Localstorage> getFindAllId(int superiorId){
        return localstorageService.getFindAllID(superiorId);
    }

}
